var {
  appointment,
  getdeparmentDay,
  selectDay
} = require('../../utils/api');
const methods = require('../../utils/methods');
const app = getApp();
Page({

  data: {
    markCalendarList: [],
    defaultDate: '', //默认时间
    flag: false,
    mobile: '',
    uname:'',
    day: '',
    am: '',
    pm: '',
    id: '', //预约部门的id
    name: '', //预约部门的名字
  },

  onLoad: function (options) {
    let _this = this;
    console.log('传过来的===', options.id, options.name)
    _this.setData({
      id: options.id, //部门id
      name: options.name, //部门名字
      uname:app.globalData.userInfo.name
    })

    //获取当前部门可预约的日历
    getdeparmentDay(options.id).then((res) => {
      let array = [];
      if (res[1].length > 0) {
        for (let i = 0; i < res[1].length; i++) {
          let obj = {
            'date': res[1][i],
            'pointColor': '#0967FF'
          }
          array.push(obj)
        }
        console.log(array);
        _this.setData({
          markCalendarList: array
        })
      }
    })

    //查看当天的可预约数量
    // 获取当前日期
    let date = new Date();
    // 获取当前月份
    let nowMonth = date.getMonth() + 1;
    // 获取当前是几号
    let strDate = date.getDate();
    // 添加分隔符“-”
    let seperator = "-";
    // 对月份进行处理，1-9月在前面添加一个“0”
    if (nowMonth >= 1 && nowMonth <= 9) {
      nowMonth = "0" + nowMonth;
    }
    // 对月份进行处理，1-9号在前面添加一个“0”
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    // 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
    let nowDate = date.getFullYear() + seperator + nowMonth + seperator + strDate;
    
    _this.setData({
      day:nowDate
    })
    selectDay(_this.data.id, _this.data.name, nowDate).then((res) => {
      if (res[0] == 200 && res[1].content.length > 0) {
        _this.setData({
          flag: true,
          am: res[1].content[0].amsum,
          pm: res[1].content[0].pmsum
        })
      } else {
        _this.setData({
          flag: false
        })
      }
    })
  },
  nameInput(e){
    console.log('e.detail.value==',e.detail.value)
    this.setData({
      mobile: e.detail.value
    })
  },
  //点击日历选中某一天
  onSelect: function (e) {
    let _this = this;
    const {
      day
    } = e.detail
    console.log('onSelect', day);
    // 根据日期查询当前部门当天有没有号可约
    selectDay(_this.data.id, _this.data.name, day).then((res) => {
      if (res[0] == 200 && res[1].content.length > 0) {
        _this.setData({
          flag: true,
          am: res[1].content[0].amsum,
          pm: res[1].content[0].pmsum
        })
      } else {
        _this.setData({
          flag: false
        })
      }


    })
    _this.setData({
      day: day
    })
  },
  onRangeDate: function (e) {
    const {
      beginTime,
      endTime
    } = e.detail
    console.log('onRangeDate==1==', beginTime);
    console.log('onRangeDate==2==', endTime);
  },

  reserve_am() {
    let _this = this;
    if(!_this.data.mobile){
     methods.showToast('手机号未填!!!')
     return;
    }
    console.log('点击预约', _this.data.day, 'am');
    appointment({
      "userId": app.globalData.id,
      "name": app.globalData.userInfo['name'],
      "area": "",
      "department": _this.data.id,
      "departmentday": _this.data.day,
      "amsum": 1,
      "pmsum": 0,
      "tel":_this.data.mobile
    }).then((res) => {
      if (res[0] == 200) {
        methods.showToast('预约成功');
        wx.navigateBack({
          delta: 2,
        })
      }else{
        methods.showToast('不可重复预约')
      }
    })
  },
  reserve_pm() {
    let _this = this;
    if(!_this.data.mobile){
      methods.showToast('手机号未填!!!')
      return;
     }
    appointment({
      "userId": app.globalData.id,
      "name": app.globalData.userInfo['name'],
      "area": "",
      "department": _this.data.id,
      "departmentday": _this.data.day,
      "amsum": 0,
      "pmsum": 1,
      "tel":_this.data.mobile
    }).then((res) => {
      if (res[0] == 200) {
        methods.showToast('预约成功');
        wx.navigateBack({
          delta: 2,
        })
      }else{
        methods.showToast('不可重复预约')
      }
    })
  }
})